#include "PrecompiledHeaders.h"
#include "Configuration.h"
#include "Constants.h"

Configuration::Configuration()
{
    // load default settings
}

void Configuration::load(Ogre::String filename) 
{
	Ogre::ConfigFile cf;
	Ogre::String sec_name;
    cf.load(filename);

	Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
    while (seci.hasMoreElements())
    {
        sec_name = seci.peekNextKey();
        Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
        Ogre::ConfigFile::SettingsMultiMap::iterator seti;
        if (sec_name == "video") {
		    for (seti = settings->begin(); seti != settings->end(); ++seti)
            {
	            if (seti->first == "renderSystem")
	                render_system = seti->second;
	            else if (seti->first == "resolution")
		            sscanf(seti->second.c_str(), "%dx%d", &res_w, &res_h);
	            else if (seti->first == "fullScreen")
		            full_screen = (seti->second == "true");
		        else if (seti->first == "triScreen")
		            tri_screen = (seti->second == "true");
		        else 
		            video_options[seti->first] = seti->second;
            }
		} else if (sec_name == "sound")
		{
		    for (seti = settings->begin(); seti != settings->end(); ++seti)
		    {
		        if (seti->first == "sfx")
		            sscanf(seti->second.c_str(), "%f", &volumeSFX);
	            else if (seti->first == "bgm")
		            sscanf(seti->second.c_str(), "%f", &volumeBGM);
		    }
		} else if (sec_name == "gameplay")
		{
		    for (seti = settings->begin(); seti != settings->end(); ++seti)
		    {
		        if (seti->first == "skyboxrandom")
	                skyboxrandom = (seti->second == "true");
		    }
		}
    }
}

void Configuration::save(Ogre::String filename)
{
    std::ofstream outputFile;
    outputFile.open(filename.c_str());
    if(!outputFile.is_open()) {
        fprintf(stderr, "Error: could not open high score file %s for writing", filename.c_str());
    }
    outputFile << "[video]\n";
    for (Ogre::NameValuePairList::iterator it = video_options.begin(); it != video_options.end(); it++)
    {
        outputFile << it->first << "=" << it->second << "\n";   
    }
    outputFile << "renderSystem=" << render_system << "\n";
    outputFile << "resolution=" << res_w << "x" << res_h << "\n";
    outputFile << "fullScreen=";
    if (!full_screen) outputFile << "false\n";  
    else outputFile << "true\n";
    outputFile << "triScreen=";
    if (!tri_screen) outputFile << "false\n\n";  
    else outputFile << "true\n\n";
    outputFile << "[sound]\n";
    outputFile << "sfx=" << volumeSFX << "\n";
    outputFile << "bgm=" << volumeBGM << "\n\n";
    outputFile << "[gameplay]\n";
    outputFile << "skyboxrandom=";
    if (!skyboxrandom) outputFile << "false\n";  
    else outputFile << "true\n";
    outputFile.close();
}

// Sets the FSM
void Configuration::setFSM(bool isFSM)
{
	full_screen = isFSM;
	if (isFSM)
	{
		video_options["fullScreen"] = "true";
	}
	else
	{
		video_options["fullScreen"] = "false";
	}
}
